package com.hpq.websocket.service.adapter;

import com.hpq.chat.domain.dto.ChatMsgRecallDTO;
import com.hpq.common.domain.enums.YesOrNoEnum;
import com.hpq.user.domain.model.User;
import com.hpq.websocket.domain.enums.WSRespTypeEnum;
import com.hpq.websocket.domain.vo.response.*;
import jodd.bean.BeanUtil;
import lombok.Builder;
import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket;
import org.springframework.beans.BeanUtils;

@Builder
public class WebSocketAdapter {
    /**
     * 组装登录二维码响应
     * @param ticket
     * @return
     */
    public static WSBaseResp<?> buildResp(WxMpQrCodeTicket ticket) {
        WSBaseResp<WSLoginUrl> resp = new WSBaseResp<>();
        resp.setType(WSRespTypeEnum.LOGIN_URL.getType());
        resp.setData(new WSLoginUrl(ticket.getUrl()));
        return resp;
    }

    /**
     * 组装登录成功响应
     *
     * @param user
     * @param token
     * @param b
     * @return
     */
    public static WSBaseResp<?> buildLoginResp(User user, String token, boolean hasPower) {
        WSBaseResp<WSLoginSuccess> resp = new WSBaseResp<>();
        resp.setType(WSRespTypeEnum.LOGIN_SUCCESS.getType());
        WSLoginSuccess wsLoginSuccess = WSLoginSuccess.builder()
                .avatar(user.getAvatar())
                .uid(user.getId())
                .name(user.getName())
                .token(token)
                .power(hasPower ? YesOrNoEnum.YES.getStatus() : YesOrNoEnum.NO.getStatus())
                .build();
        resp.setData(wsLoginSuccess);
        return resp;
    }

    /**
     *  组装扫码登录成功响应
     * @return
     */
    public static WSBaseResp buildScanSuccessResp() {
        WSBaseResp wsBaseResp = new WSBaseResp();
        wsBaseResp.setType(WSRespTypeEnum.LOGIN_SCAN_SUCCESS.getType());
        return wsBaseResp;
    }

    /**
     * 组装用户扫码登录成功等待授权的响应
     * @return
     */
    public static WSBaseResp buildWaitAuthorizeResp() {
        WSBaseResp<Object> wsBaseResp = new WSBaseResp<>();
        wsBaseResp.setType(WSRespTypeEnum.LOGIN_SCAN_SUCCESS.getType());
        return wsBaseResp;
    }

    /**
     * 组装token失效响应
     * @return
     */
    public static WSBaseResp<?> buildInvalidTokenResp() {
        WSBaseResp<Object> wsBaseResp = new WSBaseResp<>();
        wsBaseResp.setType(WSRespTypeEnum.INVALIDATE_TOKEN.getType());
        return wsBaseResp;
    }

    /**
     *  组装黑名单响应
     * @param user
     * @return
     */
    public static WSBaseResp<?> buildBlackResp(User user) {
        WSBaseResp<WSBlack> resp = new WSBaseResp<>();
        resp.setType(WSRespTypeEnum.BLACK.getType());
        WSBlack wsBlack = WSBlack.builder()
                .uid(user.getId())
                .build();
        resp.setData(wsBlack);
        return resp;
    }

    public static WSBaseResp<?> buildMsgRecall(ChatMsgRecallDTO recallDTO) {
        WSBaseResp<WSMsgRecall> resp = new WSBaseResp<>();
        resp.setType(WSRespTypeEnum.RECALL.getType());
        WSMsgRecall recall = new WSMsgRecall();
        BeanUtils.copyProperties(recallDTO, recall);
        resp.setData(recall);
        return resp;
    }
}
